package com.cjkj.equipment.controller;

import com.cjkj.common.annotation.Log;
import com.cjkj.common.core.controller.BaseController;
import com.cjkj.common.core.domain.AjaxResult;
import com.cjkj.common.core.page.TableDataInfo;
import com.cjkj.common.enums.BusinessType;
import com.cjkj.common.utils.poi.ExcelUtil;
import com.cjkj.equipment.domain.Equipment;
import com.cjkj.equipment.domain.vo.EquipmentVo;
import com.cjkj.equipment.service.IEquipmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备信息Controller
 * 
 * @author cjkj
 * @date 2024-08-16
 */
@Controller
@RequestMapping("/equipment")
public class EquipmentController extends BaseController
{
    private final String prefix = "business/equipment";

    @Resource
    private IEquipmentService equipmentService;

    @RequiresPermissions("business:equipment:view")
    @GetMapping()
    public String equipment()
    {
        return prefix + "/list";
    }

    /**
     * 查询设备信息列表
     */
    @RequiresPermissions("business:equipment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Equipment equipment)
    {
        startPage();
        List<EquipmentVo> list = equipmentService.selectEquipmentList(equipment);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @RequiresPermissions("business:equipment:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Equipment equipment) {
        List<EquipmentVo> list = equipmentService.selectExportEquipmentList(equipment);
        ExcelUtil<EquipmentVo> util = new ExcelUtil<>(EquipmentVo.class);
        return util.exportExcel(list, "设备信息数据");
    }

    /**
     * 新增设备信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备信息
     */
    @RequiresPermissions("business:equipment:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Equipment equipment)
    {
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设备信息
     */
    @RequiresPermissions("business:equipment:update")
    @GetMapping("/update/{id}")
    public String update(@PathVariable("id") Long id, ModelMap mmap)
    {
        Equipment equipment = equipmentService.selectEquipmentById(id);
        mmap.put("equipment", equipment);
        return prefix + "/update";
    }

    /**
     * 修改保存设备信息
     */
    @RequiresPermissions("business:equipment:update")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult editSave(Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设备信息
     */
    @RequiresPermissions("business:equipment:delete")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @PostMapping( "/delete")
    @ResponseBody
    public AjaxResult delete(String ids)
    {
        return toAjax(equipmentService.deleteEquipmentByIds(ids));
    }

    /**
     * 验证设备号是否存在
     * @param equipment
     * @return
     */
    @PostMapping("checkEquipmentNoUnique")
    @ResponseBody
    public boolean checkEquipmentNoUnique(Equipment equipment) {
        return equipmentService.checkEquipmentNoUnique(equipment);
    }

    /**
     * 导入设备
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "设备信息", businessType = BusinessType.IMPORT)
    @RequiresPermissions("business:equipment:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<EquipmentVo> util = new ExcelUtil<>(EquipmentVo.class);
        List<EquipmentVo> equipmentList = util.importExcel(file.getInputStream());
        String message = equipmentService.importData(equipmentList);
        return AjaxResult.success(message);
    }

    /**
     * 获取导入模板
     * @return
     */
    @RequiresPermissions("business:equipment:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<EquipmentVo> util = new ExcelUtil<>(EquipmentVo.class);
        return util.importTemplateExcel("设备导入模板");
    }
}
